<template>
	<view class="login">
		<image class="bg" src="../../static/kp/login-bg.png"></image>
		<view class="navbar">
			<uni-nav-bar :border="false" title="绑定手机号" :statusBar="true" color="#000" backgroundColor="rgba(0,0,0,0)" left-icon="left"
				@clickLeft="navBack" />
		</view>
		<view class="header">
			<image class="logo" src="../../static/logo.png"></image>
			<text class="name">人才直聘</text>
			<text class="tips">绑定手机号，体验更多精彩</text>
		</view>
		<view class="content">
			<view class="main">
				<view class="phone">
					<text class="tit">+86</text>
					<view class="line"></view>
					<input type="number" maxlength="11" v-model="phoneData" placeholder="请输入手机号码" />
				</view>
				<view class="code">
					<input type="number" maxlength="6" placeholder="请输入验证码" v-model="verCode" @confirm="toLogin"
						style="width: 70%;" />
					<view class="codeText" v-if="coding == false" @click="getCode()">获取验证码</view>
					<view class="authTime" v-else>{{ auth_time }}秒</view>
				</view>
			</view>
			<view class="btn-login" @click="startLogin()">
				<text class="name">立即绑定</text>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import {
		mapMutations,
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				phoneData: '', //用户/电话
				verCode: '',
				code: null,
				
				coding: false,
				auth_time: 60,
			};
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		onLoad() {
		},
		mounted() {
			_this = this;
		},
		methods: {
			...mapMutations(['login']),

			//绑定手机号
			async startLogin() {
				let phoneReg = /^1[1-9][0-9]\d{8}$/;
				try {
					if (this.phoneData == '') {
						throw '请填写手机号';
					}
					if (!phoneReg.test(this.phoneData)) {
						throw '手机号格式有误';
					}
					if (this.verCode == '') {
						throw '请填写验证码';
					}
				} catch (err) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: err
					});
					return;
				}
				let params = {
					phone: this.phoneData,
					code: this.verCode,
				};
				let data = await this.$apis.bindPhone(params);
				if (data) {
					uni.showToast({
						title: '登录成功',
						icon: 'none',
						position: 'bottom'
					});
					uni.navigateBack();
				}
			},
			
			/**
			 * 获取验证码
			 */
			async getCode() {
				// 判断手机号格式是否正确
				var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (!myreg.test(this.phoneData)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号码'
					});
					return false;
				}
				//设置倒计时秒
				this.auth_time = 60;
				this.coding = true;
				var auth_timetimer = setInterval(() => {
					this.auth_time--;
					if (this.auth_time < 0) {
						this.coding = false;
						clearInterval(auth_timetimer);
					}
				}, 1000);
				//获取验证码
				let params = { 
					phone: this.phoneData,
					smsmode:1,//短信模板方式  0 .登录模板、1.注册模板、2.忘记密码模板
				};
				let data = await this.$apis.getVerifyCode(params,{isRes: true});
				console.log('getVerCode===', data);
				if (data) {
					uni.showToast({title: data.message});
				}else{
					uni.showToast({title: '获取失败,请重试',icon:'none'});
				}
			},

			//获取验证码
			async getVerCode() {
				var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (!myreg.test(this.phoneData)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号码',
						position: 'bottom'
					});
					return false;
				}
				//触发倒计时（一般用于请求成功验证码后调用）
				this.$refs.runCode.$emit('runCode');
				let params = {
					phone: this.phoneData
				};
				let data = await this.$apis.getVerifyCode(params);
				console.log('getVerCode===', data);
				if (data) {
					uni.showToast({
						title: '获取成功',
						icon: 'none',
						position: 'bottom'
					});
				}
				//终止倒计时（用于突然需要终止倒计时的场景）
				this.$refs.runCode.$emit('runCode', 0);
			},

			// 返回
			navBack() {
				uni.navigateBack({
					delta: 1
				});
				// uni.switchTab({ url: '/pages/index/index' });
			},

		}
	};
</script>

<style lang="scss">
	.bg {
		width: 750upx;
		height: 608upx;
		position: absolute;
		z-index: 0;
	}

	.header {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		text-align: center;
		width: 100%;
		height: 480upx;

		.logo {
			width: 200upx;
			height: 200upx;
			z-index: 1;
			margin-top: 5upx;
		}

		.name {
			margin-top: 30upx;
			color: #000;
			font-size: 34upx;
			font-weight: bold;
			z-index: 1;
		}

		.tips {
			margin-top: 10upx;
			color: #999;
			font-size: 26upx;
			z-index: 1;
		}
	}

	.bottom_pact {
		margin-top: 30upx;
		display: flex;
		align-items: center;
		height: 70upx;
		line-height: 70upx;
		width: 90%;

		.agreement {
			font-size: 28upx;
			color: #9B9B9B;
		}

		.agreement-text {
			text-decoration: underline;
			color: #12ae85;
			padding: 0 10upx;
			font-size: 28upx;
		}
	}

	.auth-login {
		margin-top: 40upx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.others{
		display: flex;
		flex-direction: row;
		align-items: center;
		.line{
			background-color: #eee;
			width: 200upx;
			height: 2upx;
		}
		.other{
			padding: 30upx;
			font-size: 28upx;
			color: #666;
		}
	}

	.auth-log-login {
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 50%;
		width: 80upx;
		height: 80upx;
		position: relative;
		margin: 0 35upx;
	}

	.btn-wx {
		position: fixed;
		opacity: 0;
		border-radius: 50px;
		background: #2ba245;
		color: #fff;
		font-size: $font-lg;
	}
	
	.btn-login{
		margin-top: 30upx;
		border-radius: 30rpx;
		font-size: 35rpx;
		width: 90%;
		background-image: linear-gradient(90deg, #7FB6FF, #7FF1FF);
		color: #ffffff;
		align-items: center;
		display: flex;
		justify-content: center;
		height: 80upx;
		
		.name {
			font-size: 32upx;
		}
	}

	.content {
		display: flex;
		flex-direction: column;
		justify-content: center;
		/* margin-top: 128upx; */
	}

	/* 主体 */
	.main {
		display: flex;
		flex-direction: column;
		width: 88%;
		
		input{
			padding: 20upx;
			width: 100%;
		}
		.phone{
			display: flex;
			flex-direction: row;
			align-items: center;
			border-bottom: 1upx solid #eee;
			padding: 10upx 10upx;
			
			.line{
				height: 30upx;
				width: 2upx;
				background-color: #999;
				margin: 0 20upx;
			}
			
			.tit{
				color: #000;
			}
		}
		
		.code {
			display: flex;
			flex-direction: row;
			align-items: center;
			width: 100%;
			box-sizing: border-box;
			justify-content: space-between;
			border-bottom: 1upx solid #eee;
			padding: 10upx 10upx;
			
			.codeText{
				color: $main-color;
			}
		}
	}

	.tips {
		color: #999999;
		font-size: 28upx;
		margin-top: 64upx;
		margin-left: 48upx;
	}

	/* 其他登录方式 */
	.other_login {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 256upx;
		text-align: center;
	}

	.login_icon {
		border: none;
		font-size: 64upx;
		margin: 0 64upx 0 64upx;
		color: rgba(0, 0, 0, 0.7);
	}

	.wechat_color {
		color: #83dc42;
	}

	.weibo_color {
		color: #f9221d;
	}

	.github_color {
		color: #24292e;
	}

	/* 底部 */
	.footer {
		display: flex;
		flex-direction: row;
		justify-content: flex-end;
		align-items: center;
		width: 88%;
		font-size: 28upx;
		margin-top: 30upx;
		color: #999;
		text-align: center;
		height: 40upx;
		line-height: 40upx;
	}

	.footer text {
		font-size: 24upx;
		margin-left: 15upx;
		margin-right: 15upx;
	}
</style>